package com.weiku.until;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FileUntil {
	final static Logger log = LoggerFactory.getLogger(FileUntil.class);

	public static List<String> traverseFolder(String path) {
        File file = new File(path);
        List<String> list=new ArrayList<String>();
        String filepath="";
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null == files || files.length == 0) {
                System.out.println("文件夹是空的!");
                return list;
            } else {
            	 try {
	                for (File file2 : files) {
	                    if (file2.isDirectory()) {
	                        System.out.println("文件夹:" + file2.getAbsolutePath());
	                        list=traverseFolder(file2.getAbsolutePath());
	                    } else {
	                        System.out.println("文件:" + file2.getAbsolutePath());
	                        list.add(file2.getAbsolutePath());
	                    }
	                }
            	} catch (Exception e) {
					e.printStackTrace();
				}
            }
        } else {
            System.out.println("文件不存在!");
            return list;
        }
		return list;
    }
	public static void renameFile(String path,String oldname,String newname){
        if(!oldname.equals(newname)){//新的文件名和以前文件名不同时,才有必要进行重命名
            File oldfile=new File(path+"/"+oldname);
            File newfile=new File(path+"/"+newname);
            if(newfile.exists())//若在该目录下已经有一个文件和新文件名相同，则不允许重命名
                System.out.println(newname+"已经存在！");
            else{
                oldfile.renameTo(newfile);
            }
        }         
    }
	
	public static String ReadFile(String path){
	        StringBuilder result = new StringBuilder();
	        try{
	        	File file=new File(path);
	            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
	            int count=0;  
	            long num=0;  
                while((count=br.read())!=-1){  
                    result.append((char)count);
                    num++;  
	            }     
	            br.close();    
	        }catch(Exception e){
	            e.printStackTrace();
	        }
	        return result.toString();
	   }

	public static void copyOrderXmlFile(String srcPathStr, String desPathStr) throws IOException, ParseException {
        String newFileName = srcPathStr.substring(srcPathStr.lastIndexOf("\\")+1); //目标文件地址
        Map<String,String> map=XML.parseOrderReceiptXml(srcPathStr);
		 String fileName=map.get("orderNo")+"-"+map.get("returnStatus")+"-"+map.get("returnTime");
	     System.out.println(fileName);
        desPathStr = desPathStr + File.separator +fileName+"-"+newFileName; //源文件地址
        System.out.println(desPathStr);
        FileInputStream fis = new FileInputStream(srcPathStr);
        FileOutputStream fos = new FileOutputStream(desPathStr);                
        byte datas[] = new byte[1024*8];
        int len = 0;
        while((len = fis.read(datas))!=-1){
            fos.write(datas,0,len);
        }
        fis.close();
        fis.close();
        File file=new File(srcPathStr);
        if(file.exists()){
       	 file.delete();
       	 log.info("删除文件："+srcPathStr);
        }
	}

	public static void copyListXmlFile(String srcPathStr, String desPathStr) throws IOException, ParseException, SQLException {
		String newFileName = srcPathStr.substring(srcPathStr.lastIndexOf("\\")+1); //目标文件地址
        Map<String,String> map=XML.parseListReceiptXml(srcPathStr);
		 String fileName=map.get("orderNo")+"-"+map.get("returnStatus")+"-"+map.get("returnTime");
	     System.out.println(fileName);
        desPathStr = desPathStr + File.separator +fileName+"-"+newFileName; //源文件地址
        System.out.println(desPathStr);
        FileInputStream fis = new FileInputStream(srcPathStr);
        FileOutputStream fos = new FileOutputStream(desPathStr);                
        byte datas[] = new byte[1024*8];
        int len = 0;
        while((len = fis.read(datas))!=-1){
            fos.write(datas,0,len);
        }
        fis.close();
        fis.close();
        File file=new File(srcPathStr);
        if(file.exists()){
       	 file.delete();
       	 log.info("删除文件："+srcPathStr);
        }
	}

	public static void copyPaybillXmlFile(String srcPathStr, String desPathStr) throws IOException, ParseException {
		String newFileName = srcPathStr.substring(srcPathStr.lastIndexOf("\\")+1); //目标文件地址
        Map<String,String> map=XML.parsePaybillReceiptXml(srcPathStr);
		 String fileName=map.get("payNo")+"-"+map.get("returnStatus")+"-"+map.get("returnTime");
	     System.out.println(fileName);
        desPathStr = desPathStr + File.separator +fileName+"-"+newFileName; //源文件地址
        System.out.println(desPathStr);
        FileInputStream fis = new FileInputStream(srcPathStr);
        FileOutputStream fos = new FileOutputStream(desPathStr);                
        byte datas[] = new byte[1024*8];
        int len = 0;
        while((len = fis.read(datas))!=-1){
            fos.write(datas,0,len);
        }
        fis.close();
        fis.close();
        File file=new File(srcPathStr);
        if(file.exists()){
       	 file.delete();
       	 log.info("删除文件："+srcPathStr);
        }
	}

	public static void copyWaybillXmlFile(String srcPathStr, String desPathStr) throws IOException, ParseException {
		String newFileName = srcPathStr.substring(srcPathStr.lastIndexOf("\\")+1); //目标文件地址
        Map<String,String> map=XML.parseWaybillReceiptXml(srcPathStr);
		 String fileName=map.get("logisticsNo")+"-"+map.get("returnStatus")+"-"+map.get("returnTime");
	     System.out.println(fileName);
        desPathStr = desPathStr + File.separator +fileName+"-"+newFileName; //源文件地址
        System.out.println(desPathStr);
        FileInputStream fis = new FileInputStream(srcPathStr);
        FileOutputStream fos = new FileOutputStream(desPathStr);                
        byte datas[] = new byte[1024*8];
        int len = 0;
        while((len = fis.read(datas))!=-1){
            fos.write(datas,0,len);
        }
        fis.close();
        fis.close();
        File file=new File(srcPathStr);
        if(file.exists()){
       	 file.delete();
       	 log.info("删除文件："+srcPathStr);
        }
	}

}

